/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors:
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 * #asset(ui_mov/*) #asset(qx/mobile/icon/${qx.mobile.platform}/*)
 * #asset(qx/mobile/icon/common/*)
 * 
 ******************************************************************************/

/**
 * This is the main application class of your custom application "ui_mov"
 */
qx.Class.define("ui_mov.views.noticias", {
	extend : qx.ui.mobile.page.NavigationPage,
	construct : function() {
		this.base(arguments);
		this.set({
					title : "Noticias", // will be replaced by username
					showBackButton : true,
					backButtonText : "Volver"
				});

		var format = function(d) {
			return (d.getDate() + "/" + d.getMonth() + "/" + d.getFullYear());
		};
		var list = new qx.ui.mobile.list.List({
					configureItem : function(item, data, row) {
						item.setTitle(data.titulo);
						item.setSubtitle(format(new Date(data.fecha)));
						item.setSelectable(true);
					}
				});
		this.list = list;
		list.setModel(new qx.data.Array([]));
		// var busyIndicator = new
		// qx.ui.mobile.dialog.BusyIndicator("Cargando
		// Temas...");
		// qx.core.Init.getApplication().getRoot().add(busyIndicator);
		list.addListener("changeSelection", function(evt) {
					var noticia = list.getModel().getItem(evt.getData());
					window.pages.noticia.setTitle(noticia.titulo);
					window.pages.noticia.setNoticia(noticia.fecha, noticia.contenido);
					window.pages.noticia.show();
				}, this);

		this.addListener("back", function() {
					window.pages.noticiasroot.show({
								reverse : true
							});
				}, this);
		this.add(list);
	},
	members : {
		refreshlist : function() {
			var req = new qx.io.request.Xhr("/rpc/noticia/listar");
			req.setRequestData({
						'idTema' : this.idTema
					});
			req.addListener("success", function(e) {
						var res = e.getTarget().getResponse();
						this.list.setModel(new qx.data.Array(res));
					}, this);
			req.send();
		},
		idTema : -1,
		load : function(idTema) {
			this.idTema = idTema;
			this.refreshlist();
		}
	}
});
